using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace ProjetoFinal
{
    public class Manager : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        Menu menu;
        Credits credits;
        Level1 level1;
        Level2 level2;
        Level3 level3;
        GameOver gameOver;
        Instructions instructions;
        Story story;
        public static int levelNow;

        public KeyboardState nowKeyboard, oldKeyboard;


        public enum GameState
        {
            Menu, Game, Credits, Quit, GameOver, Instructions, Story, Level1, Level2, Level3
        }

        public static GameState nowState;


        public Manager()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
            IsMouseVisible = true;
            graphics.PreferredBackBufferHeight = 768;
            graphics.PreferredBackBufferWidth = 1024;
            
        }


        protected override void Initialize()
        {
            nowState = GameState.Menu;
            menu = new Menu();
            menu.Initialize();
            level1 = new Level1();
            level1.Initialize();
            level2 = new Level2();
            level2.Initialize();
            level3 = new Level3();
            level3.Initialize();
            gameOver = new GameOver();
            credits = new Credits();
            instructions = new Instructions();
            story = new Story();

            base.Initialize();
        }

        protected override void LoadContent()
        {
            spriteBatch = new SpriteBatch(GraphicsDevice);
            menu.LoadContent(Content);
            credits.LoadContent(Content);
            level1.LoadContent(Content);
            level2.LoadContent(Content);
            level3.LoadContent(Content);
            gameOver.LoadContent(Content);
            instructions.LoadContent(Content);
            story.LoadContent(Content);
        }


        protected override void Update(GameTime gameTime)
        {
            nowKeyboard = Keyboard.GetState();

            switch (nowState)
            {
                case GameState.Menu:
                    menu.Update(gameTime, nowKeyboard, oldKeyboard, level1);
                    break;

                case GameState.Credits:
                    credits.Update(nowKeyboard, oldKeyboard);
                    break;

                case GameState.Quit:
                    this.Exit();
                    break;

                case GameState.GameOver:
                    gameOver.Update(gameTime);
                    break;
                case GameState.Instructions:
                    instructions.Update(nowKeyboard,oldKeyboard);
                    break;

                case GameState.Story:
                    story.Update(nowKeyboard, oldKeyboard);
                    break;

                case GameState.Level1:
                    level1.Update(nowKeyboard, oldKeyboard, gameOver);
                    break;

                case GameState.Level2:
                    level2.Update(nowKeyboard, oldKeyboard, gameOver);
                    break;

                case GameState.Level3:
                    level3.Update(nowKeyboard, oldKeyboard, gameOver);
                    break;


            }

            oldKeyboard = nowKeyboard;

            base.Update(gameTime);
        }



        protected override void Draw(GameTime gameTime)
        {
            
                GraphicsDevice.Clear(Color.CornflowerBlue);

                switch (nowState)
                {
                    case GameState.Menu:
                        menu.Draw(gameTime, spriteBatch);

                        break;


                    case GameState.Credits:
                        credits.Draw(spriteBatch);

                        break;

                    case GameState.GameOver:
                        if(levelNow == 1)
                        gameOver.Draw(spriteBatch, Level1.score);

                        if (levelNow == 2)
                            gameOver.Draw(spriteBatch, Level2.score);

                        if (levelNow == 3)
                            gameOver.Draw(spriteBatch, Level3.score);
                        break;

                    case GameState.Instructions:
                        instructions.Draw(spriteBatch);
                        break;

                    case GameState.Story:
                        story.Draw(spriteBatch);
                        break;

                    case GameState.Level1:
                        level1.Draw(spriteBatch);
                        break;

                    case GameState.Level2:
                        level2.Draw(spriteBatch);
                        break;

                    case GameState.Level3:
                        level3.Draw(spriteBatch);
                        break;

                }


                base.Draw(gameTime);
            
           
        }
    }
}
